package br.ufrgs.tcp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
import br.ufrgs.tcp.model.Predio;
import br.ufrgs.tcp.model.Sala;

public class SearchController {

	private static Cursor predios;
	private static Cursor salas;

	public static void performSearch(String query, Activity context) {

		predios = Predio.fetchByNumero(query);
		salas = Sala.fetchByNumero(query);

		if (predios.getCount() > 0 && salas.getCount() > 0) 
			mostraDialogResult(context,predios.getCount(),salas.getCount(), query);	
		else if (predios.getCount() > 0)
			mostrPredios(context,predios);
		else if (salas.getCount() > 0) {
			mostraSalas(context,query);
			salas.close();
		}
		else 
			Toast.makeText(context, "Nenhum resultado encontrado", Toast.LENGTH_SHORT).show();
	}

	private static void mostrPredios(Context context, Cursor predios) {
		predios.close();

	}

	private static void mostraSalas(Context context, String query) {
		Intent resultActivity = new Intent(context, ResultActivity.class);
		resultActivity.putExtra("query",query);
		context.startActivity(resultActivity);
	}

	private static void mostraDialogResult(Activity activity, int prediosCount,
			int salasCount, String query) {
		Bundle b = new Bundle();
		b.putInt("predios", prediosCount);
		b.putInt("salas", salasCount);
		b.putString("query",query);
		activity.showDialog(0, b);
	}
}
